package cadtoolbox.graphical;


import cadtoolbox.model.AbstractInput;
import cadtoolbox.model.OligoGraph;
import cadtoolbox.model.PulseInput;
import cadtoolbox.model.SequenceVertex;
import cadtoolbox.model.SequenceVertex.DNAInput;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class InputManageWindow<V> extends JFrame implements ActionListener, ListSelectionListener {
 
	
	SequenceVertex vert;
	DefaultListModel dlm;
	private InputEditWindow iew = null;
	final OligoGraph<V,?> graph;
	
	InputManageWindow(V vert, OligoGraph<V,?> graph){
		super("Input manager");
		this.vert = (SequenceVertex) vert;
		this.graph = graph;
		JPanel panel = new JPanel();
		JPanel buttonpanel = new JPanel();
		JLabel label = new JLabel("Inputs for "+vert);
		this.dlm = new DefaultListModel();
		for (AbstractInput input : this.vert.inputs){
		dlm.addElement(input);
		}
		JList jl = new JList(dlm);
		jl.addListSelectionListener(this);
		JScrollPane listScroller = new JScrollPane(jl);
		JButton addbutton = new JButton("add");
		addbutton.setActionCommand("add");
		addbutton.addActionListener(this);
		
		//JButton save = new JButton("save");
		//save.setActionCommand("save");
		//save.addActionListener(this);
		listScroller.setPreferredSize(new Dimension(250, 80));
		listScroller.setAlignmentX(LEFT_ALIGNMENT);
		
		panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

		panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
		buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.X_AXIS));

		panel.add(label);
		panel.add(Box.createRigidArea(new Dimension(0,5)));
		panel.add(listScroller);
		buttonpanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
		buttonpanel.add(Box.createHorizontalGlue());
		buttonpanel.add(addbutton);
		buttonpanel.add(Box.createRigidArea(new Dimension(10,0)));
		//buttonpanel.add(save);
		
		Container content = this.getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		
		content.add(panel);
		content.add(buttonpanel);

		this.pack();
		//System.out.println("Buttonpanel size:"+buttonpanel.getWidth()+" Panel size:"+panel.getWidth());
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getActionCommand() == "add" ){
			PulseInput newinput = new PulseInput(100,50);
			vert.inputs.add(newinput);
			dlm.addElement(newinput);
			graph.replot();
			this.repaint();
		}
		
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		if (((JList)arg0.getSource()).getSelectedValue() != null && iew == null){
		iew = new InputEditWindow(this,(AbstractInput)((JList)arg0.getSource()).getSelectedValue());
		((JList)arg0.getSource()).clearSelection();
		iew.setVisible(true);
		iew.setAlwaysOnTop(true);
		iew.addWindowListener(new WindowListener(){

			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				iew = null;
				
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		}
	}

	public void deleteInput(AbstractInput input) {
		vert.inputs.remove(input);
		this.dlm.removeElement(input);
		this.repaint();
		graph.replot();
	}
}
